package com.xhwl.centiotdata.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhwl.centiotdata.service.cent.IAccountCentService;
import com.xhwl.centiotdata.service.cent.IUserCentService;
import com.xhwl.centiotdata.suppliersystem.haikang.GeneralHaiKangHandler;
import com.xhwl.common.dto.cent.industrial.IndustrialStaffDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.config.IndustryEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.query.cent.sdata.AccountQuery;
import com.xhwl.common.vo.cent.industrial.IndustrialStaffVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: lianghz
 * @Description:
 * @Date: Create in 13:36 2021/6/3
 */
@Service
public class AccountServiceImpl {
    private static final Logger log = LoggerFactory.getLogger(GeneralHaiKangHandler.class);

    @Autowired
    private IAccountCentService accountCentService;
    @Autowired
    private IUserCentService userCentService;

    public List<AccountDTO> queryAccount(Integer projectId, List<Integer> accountIdList) {
        AccountQuery accountQuery = new AccountQuery();
        accountQuery.setProjectIdList(Arrays.asList(projectId));
        accountQuery.setIdList(accountIdList);
        log.info("queryAccount request : {}", JSON.toJSONString(accountQuery));
        List<AccountDTO> accountDTOList = accountCentService.getList(accountQuery);
        log.info("queryAccount response : {}", JSON.toJSONString(accountDTOList));
        if (CollectionUtils.isEmpty(accountDTOList)) {
            throw new BusinessException("未找到人员信息");
        }
        return accountDTOList;
    }

    private List<IndustrialStaffVO> queryPRDUser(Integer projectId, List<Integer> accountIdList) {
        IndustrialStaffDTO industrialStaffDTO = new IndustrialStaffDTO();
        industrialStaffDTO.setProjectId(projectId);
        industrialStaffDTO.setAccoutIds(accountIdList);
        log.info("queryPRDUser request : {}", JSON.toJSONString(industrialStaffDTO));
        ResultJson userResultJson = userCentService.pageList(industrialStaffDTO, IndustryEnum.PRD.id);
        log.info("queryPRDUser response : {}", JSON.toJSONString(userResultJson));
        if (userResultJson == null || 1000 != userResultJson.getCode().intValue() || userResultJson.getResult() == null) {
            log.error("queryPRDUser failed! message :{} ", userResultJson.getMessage());
            throw new BusinessException("未找到人员信息");
        }
        JSONObject pageJSONObject = (JSONObject) JSON.toJSON(userResultJson.getResult());
        Page page = JSON.toJavaObject(pageJSONObject, Page.class);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            log.error("queryPRDUser records is null! ");
            throw new BusinessException("未找到人员信息");
        }
        JSONArray userJSONArray = (JSONArray) JSON.toJSON(page.getRecords());
        List<IndustrialStaffVO> staffVOList = userJSONArray.toJavaList(IndustrialStaffVO.class);
        return staffVOList;
    }

    public UserDTO queryUser(Short industryId, Integer projectId, Integer accountId) {
        UserDTO userDTO = new UserDTO();
        if (industryId != IndustryEnum.PRD.id) {
            List<AccountDTO> accountDTO = this.queryAccount(projectId, Arrays.asList(accountId));
            BeanUtils.copyProperties(accountDTO.get(0), userDTO);
        } else {
            List<IndustrialStaffVO> staffVOList = this.queryPRDUser(projectId, Arrays.asList(accountId));
            IndustrialStaffVO staffVO = staffVOList.get(0);
            userDTO.setFaceUrl(staffVO.getAvatar());
            userDTO.setName(staffVO.getName());
        }
        return userDTO;
    }

}